package com.noodles.mall.controller;

import com.noodles.mall.entity.UserCity;
import com.noodles.mall.service.UserCityService;
import com.noodles.mall.web.JsonResult;
import com.noodles.mall.web.State;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user-city")
public class UserCityController {

    @Autowired
    private UserCityService userCityService;

    /** 增加 **/
    @RequestMapping("/insert")
    public JsonResult insert(@RequestBody UserCity userCity) {
        System.out.println("UserCityController: insert");
        System.out.println("userCity: "+userCity);
        userCity=userCityService.insert(userCity);
        return userCity!=null
                ? JsonResult.success(userCity) : JsonResult.error(State.ERR_SQL);
    }

    /** 删除: 单个 **/
    @RequestMapping("/delete")
    public JsonResult delete(@RequestBody List<Integer> ids) {
        System.out.println("UserCityController: delete");
        System.out.println("ids: "+ids);
        return userCityService.delete(ids.get(0))
                ? JsonResult.success(ids.get(0)) : JsonResult.error(State.ERR_SQL);
    }

    /** 修改 **/
    @RequestMapping("/update")
    public JsonResult update(@RequestBody UserCity userCity) {
        System.out.println("UserCityController: update");
        System.out.println("userCity: "+userCity);
        return userCityService.update(userCity)
                ? JsonResult.success(userCity.getId()) : JsonResult.error(State.ERR_SQL);
    }

    /** 查询列表: userId **/
    @RequestMapping("/selectList/{userId}")
    public JsonResult selectList(@PathVariable(value = "userId") Integer userId) {
        System.out.println("UserCityController: selectListByUserId");
        System.out.println("userId: "+userId);
        List<UserCity> userCityList = userCityService.selectList(userId);
        return JsonResult.success(userCityList);
    }



}
